﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PTCapture.Support;

namespace PTCapture.UserControls
{
    public partial class CameraRecorderControl : UserControl
    {
        #region Fields

        /// <summary>
        /// Camera to use.
        /// </summary>
        public Camera camera;

        #endregion

        #region Delegates

        /// <summary>
        /// Tie to parent form to handle buttons for starting/stopping both cameras at once.
        /// </summary>
        /// <returns></returns>
        public delegate bool customHandler();
        public event customHandler CameraStatusChanged;

        #endregion

        #region Control Events

        /// <summary>
        /// Initialization.
        /// </summary>
        public CameraRecorderControl()
        {
            InitializeComponent();

            camera = new Camera(pnlCamera1);
            camera.FrameRate = 30;
            camera.FrameSize = new System.Drawing.Size(640, 480);

            cbVideoDevices.SelectedIndexChanged += new EventHandler(cbVideoDevices_SelectedIndexChanged);
            cbAudioDevices.SelectedIndexChanged += new EventHandler(cbAudioDevices_SelectedIndexChanged);
        }

        /// <summary>
        /// Handle changing audio selection.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void cbAudioDevices_SelectedIndexChanged(object sender, EventArgs e)
        {
            CameraStatusChanged();

            if (cbAudioDevices.SelectedIndex > -1 &&
                cbVideoDevices.SelectedIndex > -1)
            {
                btnConnectCamera.Enabled = true;
            }
        }

        /// <summary>
        /// Handle changing video selection.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void cbVideoDevices_SelectedIndexChanged(object sender, EventArgs e)
        {
            CameraStatusChanged();

            if (cbAudioDevices.SelectedIndex > -1 &&
                cbVideoDevices.SelectedIndex > -1)
            {
                btnConnectCamera.Enabled = true;
            }
        }

        /// <summary>
        /// Connect the camera.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnConnectCamera_Click(object sender, EventArgs e)
        {
            try
            {
                using (new HourGlass())
                {
                    cbAudioDevices.Enabled = false;
                    cbVideoDevices.Enabled = false;

                    camera.VideoDevice = GlobalVariables.videoDevices[cbVideoDevices.SelectedIndex];
                    camera.AudioDevice = GlobalVariables.audioDevices[cbAudioDevices.SelectedIndex];

                    camera.StartCapture();

                    btnConnectCamera.Enabled = false;
                    btnStartCamera.Enabled = true;
                    btnStopCamera.Enabled = true;

                    CameraStatusChanged();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format("Could not connect camera. \n{0}\n", ex.Message, ex.StackTrace), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

                cbAudioDevices.Enabled = true;
                cbVideoDevices.Enabled = true;
            }
        }

        /// <summary>
        /// Start recording.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnStartCamera_Click(object sender, EventArgs e)
        {
            try
            {
                using (new HourGlass())
                {
                    camera.StartRecording();

                    btnConnectCamera.Enabled = false;
                    btnStartCamera.Enabled = false;
                    btnStopCamera.Enabled = true;

                    CameraStatusChanged();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format("Could not start camera. \n{0}\n", ex.Message, ex.StackTrace), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        /// <summary>
        /// Stop recording and disconnect.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnStopCamera_Click(object sender, EventArgs e)
        {
            try
            {
                using (new HourGlass())
                {
                    camera.StopRecording();
                    camera.StopCapture();

                    btnConnectCamera.Enabled = true;
                    btnStartCamera.Enabled = false;
                    btnStopCamera.Enabled = false;

                    CameraStatusChanged();

                    cbAudioDevices.Enabled = true;
                    cbVideoDevices.Enabled = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format("Could not stop camera. \n{0}\n", ex.Message, ex.StackTrace), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #endregion

        #region Support

        /// <summary>
        /// Set save and record variables.
        /// </summary>
        /// <param name="videoPath"></param>
        /// <param name="imagePath"></param>
        public void SetVariables()
        {
            camera.VideoDirectory = GlobalVariables.VideoPath;
            camera.SnapshotDirectory = GlobalVariables.ImagePath;

            cbVideoDevices.DataSource = (from v in GlobalVariables.videoDevices select v.Name).ToList();
            cbAudioDevices.DataSource = (from a in GlobalVariables.audioDevices select a.Name).ToList();

            cbVideoDevices.SelectedIndex = -1;
            cbAudioDevices.SelectedIndex = -1;

            btnConnectCamera.Enabled = false;
        }

        #endregion

    }
}
